/*
 * SafeConsoleColorSetter.cs
 * 
 * Sets console output color, using legible colors -- without assuming gray-on-black 
 * as the default console color scheme.
 */
using System;

namespace Jitsu
{
    internal static class SafeConsoleColorSetter
    {
        static ConsoleColor s_defaultForeground = Console.ForegroundColor;
        static ConsoleColor s_defaultBackground = Console.BackgroundColor;

        public static void SetColor(ConsoleColor foreground, ConsoleColor background)
        {
            // Ensure highlighting is visible even if default color is 87.
            if (background == s_defaultBackground)
            {
                // Toggle the brightness bit.
                int temp = (int)background;
                temp ^= 0x08;
                background = (ConsoleColor)temp;
            }

            // Guard against invisible output. :0
            if (foreground == background)
            {
                // Toggle the brightness bit.
                int temp = (int)foreground;
                temp ^= 0x08;
                foreground = (ConsoleColor)temp;
            }

            Console.ForegroundColor = foreground;
            Console.BackgroundColor = background;
        }

        public static void SetColor(ConsoleColor foreground)
        {
            // Guard against invisible output. :0
            if (foreground == s_defaultBackground)
            {
                // Toggle the brightness bit.
                int temp = (int)foreground;
                temp ^= 0x08;
                foreground = (ConsoleColor)temp;
            }

            Console.ForegroundColor = foreground;
        }

        public static void ResetColor()
        {
            Console.ResetColor();
        }
    }
}